package com.qzdsoft.erpcloud.controller.proj;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qzdsoft.common.domain.query.sys.QueryParams;
import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.User;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryValueService;
import com.qzdsoft.erpcloud.service.basic.MeasurementUnitService;
import com.qzdsoft.erpcloud.service.proj.MaterialAliasService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.proj.MaterialAliasInfo;
import com.qzdsoft.erpcloud.vo.query.sys.SysMenubuttonQueryParam;
import com.qzdsoft.vo.Page;
import com.qzdsoft.vo.Response;


/**
 * Created by Admin
 */
@Controller 
@RequestMapping("/eng/material/alias")
public class MaterialAliasController extends BaseController{

	@Autowired 
	private MaterialAliasService materialAliasService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private MeasurementUnitService unitService;
//	@Autowired
//	private RedisHelper redisHelper;
	@Autowired
	private DataDictionaryValueService enumValueService;
	
	
	@Permission(validate=false)
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletRequest request, Model model, QueryParams params) {
		return "eng/materialalias/index";
	}
	
	@Permission(validate=false)
	@RequestMapping("page")
	@ResponseBody
	public Object page(HttpServletRequest request, Model model, SysMenubuttonQueryParam params) {
		LoginUser loginUser = sysUserService.getUser(getUid());
		Page<MaterialAliasInfo> menuPage = materialAliasService.findPage(params, loginUser);
		return menuPage;
	}
	
//	/**
//     * 查询物料信息（网格数据）
//     * @param request
//     * @param response
//     * @return
//     * @throws IOException
//     */
//    @ResponseBody
//    @RequestMapping("/data")
//    public Object data(HttpServletRequest request,HttpServletResponse response) throws IOException {
//    	 String jsondata = "{\"page\":\"1\"," + " \"total\":2," + " \"records\":\"13\"," + " \"rows\":" + " [" + " {" + " \"id\":\"13\"," + " \"cell\":" + " [\"13\",\"2007-10-06\",\"Client 3\",\"1000.00\",\"0.00\",\"1000.00\",null]" + " }," + " {" + " \"id\":\"12\"," + " \"cell\":" + " [\"12\",\"2007-10-06\",\"Client 2\",\"700.00\",\"140.00\",\"840.00\",null]" + " }," + " {" + " \"id\":\"11\"," + " \"cell\":" + " [\"11\",\"2007-10-06\",\"Client 1\",\"600.00\",\"120.00\",\"720.00\",null]" + " }," + " {" + " \"id\":\"10\"," + " \"cell\":" + " [\"10\",\"2007-10-06\",\"Client 2\",\"100.00\",\"20.00\",\"120.00\",null]" + " }," + " {" + " \"id\":\"9\"," + " \"cell\":" + " [\"9\",\"2007-10-06\",\"Client 1\",\"200.00\",\"40.00\",\"240.00\",null]" + " }," + " {" + " \"id\":\"8\"," + " \"cell\":" + " [\"8\",\"2007-10-06\",\"Client 3\",\"200.00\",\"0.00\",\"200.00\",null]" + " }," + " {" + " \"id\":\"7\"," + " \"cell\":" + " [\"7\",\"2007-10-05\",\"Client 2\",\"120.00\",\"12.00\",\"134.00\",null]" + " }," + " {" + " \"id\":\"6\"," + " \"cell\":" + " [\"6\",\"2007-10-05\",\"Client 1\",\"50.00\",\"10.00\",\"60.00\",\"\"]" + " }," + " {" + " \"id\":\"5\"," + " \"cell\":" + " [\"5\",\"2007-10-05\",\"Client 3\",\"100.00\",\"0.00\",\"100.00\",\"no tax at all\"]" + " }," + " {" + " \"id\":\"4\"," + " \"cell\":" + " [\"4\",\"2007-10-04\",\"Client 3\",\"150.00\",\"0.00\",\"150.00\",\"no tax\"]" + " }" + " ]," + " \"userdata\":{\"amount\":3220,\"tax\":342,\"total\":3564,\"name\":\"Totals:\"}" + " }"; 
//    	 //String jsondata = bomService.getJqGridJsonData();
//    	 response.getWriter().write(jsondata);
//    	 return null;
//    }

	/**
	 * 打开新增页面时加载 枚举类型数据
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toAddIndex", method = RequestMethod.GET)
	public String toAddIndex(Model model, HttpServletRequest request) {
		// 所有的上级菜单，url为空，type=1
		// 获取公司信息 列表
//		model.addAttribute("companyId", redisHelper.getActCenterCompany().get(0).getId());
//		model.addAttribute("companyName", redisHelper.getActCenterCompany().get(0).getName());
		//model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
		// 获取UnitList
		//model.addAttribute("unitList", unitService.getAllUnit());
		return "eng/materialalias/add/index";
	}

	//
	@ResponseBody
	@RequestMapping(value = "/addMenu", method = RequestMethod.POST)
	public Response addMenu(@ModelAttribute MaterialAliasInfo tProjMaterialAliasVi)
			throws Exception {
	    LoginUser loginUser = sysUserService.getUser(getUid());		//tProjMaterialAliasVi.setCreatorId(loginUser.getId());
		//tProjMaterialAliasVi.setCreatorName(loginUser.getName());    //setReviser(getSessionUser().getUserName());
		// 提交保存
		return materialAliasService.addMaterialAlias(tProjMaterialAliasVi, loginUser);
	}

	// @RequestMapping(value = "getDetail/{menuId}", method = RequestMethod.GET)
	// public String getDetail(@PathVariable("menuId") Integer menuId, Model
	// model) {
	// model.addAttribute("entity", menuService.getById(menuId));
	// //所有的上级菜单，url为空，type=1
	// model.addAttribute("catalogMenuList",menuService.getAllTopMenu());
	// return "/eng/materialinfo/detail/index";
	// }
	/**
	 * 查看详情
	 * 
	 * @param id
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toEdit/{id}", method = RequestMethod.GET)
	public String toEdit(@PathVariable("id") Integer id, Model model) {
	    LoginUser loginUser = getUser();		// 获取选中记录详情
		model.addAttribute("entity", materialAliasService.getById(id));
		// 获取布尔类型值
		model.addAttribute("yesNo", enumValueService.getByEnumId(id));
		// 获取公司信息
//		model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
		// 获取UnitList
		model.addAttribute("unitList", unitService.getAllUnit());
		return "eng/materialalias/edit/index";
	}

	/**
	 * 物料别名设置
	 * 修改
	 * @param tProjMaterialAliasVi
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editMaterial", method = RequestMethod.POST)
	@ResponseBody
	public Response editMenu(@ModelAttribute MaterialAliasInfo tProjMaterialAliasVi, HttpServletRequest request) {
	    LoginUser loginUser = getUser();
		return materialAliasService.editMaterialAlias(tProjMaterialAliasVi, loginUser);
	}

	/**
	 * 删除物料信息
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteMaterial", method = RequestMethod.POST)
	public Response deleteMenu(Integer id) {
		// 检查session
	    LoginUser loginUser = getUser();
		return materialAliasService.delteMaterialAlias(id, loginUser);
	}
	/**
	 * 获取网格数据
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/data")
	public Object data(HttpServletRequest request,HttpServletResponse response,int page,int rows) throws IOException{
		String jsondata = materialAliasService.getJqGridJsonData(rows ,page);
		response.setContentType("text/JavaScript; charset=utf-8");
		response.getWriter().write(jsondata);
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/partnerdata")
	public Object partnerData(HttpServletRequest request,HttpServletResponse response,int page,int rows) throws IOException{
		String jsondata = materialAliasService.getPartnerGridJsonData(rows, page);
		response.setContentType("text/JavaScript; charset=utf-8");
		response.getWriter().write(jsondata);
		return null;
		
	}
}
